from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import date, timedelta
from meiduo_admin import my_serializers
from goods.models import GoodsVisitCount
from users.models import User
from rest_framework.permissions import IsAdminUser


class UserTotalCountView(APIView):
    """
    统计总用户数量,使用APIView视图类
    """
    # 校验管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前的日期
        now_date = date.today()
        # 获取所有用户总数
        usercount = User.objects.all().count()
        # 返回用户数据
        return Response({
            'count': usercount,
            'date': now_date
        })


class UserDayCountView(APIView):
    """
    统计日增用户总数
    """
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        print(now_date)
        # 获取当天新增用户数据,__gte大于等于的意思
        usercount = User.objects.filter(date_joined__gte=now_date).count()
        # 返回数据
        return Response({
            'count': usercount,
            'date': now_date
        })


class UserActiveCountView(APIView):
    """
    日用户活跃数量统计
    """
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日活跃用户数量
        usercount = User.objects.filter(last_login__gte=now_date).count()
        return Response({
            "count": usercount,
            "date": now_date
        })


class UserOrderCountView(APIView):
    """
    日下单用户统计数量
    """
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日下单用户数量 关联过滤查询 (多模型类条件查询一模型类数据:关联模型类名小写__属性名__条件运算符=值)
        usercount = User.objects.filter(orders__create_time__gte=now_date).count()
        return Response({
            "count": usercount,
            "date": now_date
        })


class UserMonthCountView(APIView):
    """
    日分类商品访问量月增用户统计
    """
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取一个月前的日期
        start_date = now_date - timedelta(29)
        # 创建空列表保存每天的用户量
        date_list = []

        for i in range(30):
            # 获取当天日期数据
            index_date = start_date + timedelta(days=i)
            # 指定下一天日期
            next_date = start_date + timedelta(days=i + 1)
            # 查询条件： 查询的数据是大于当前日期小于明天日期的用户，得到当天用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            date_list.append(
                {
                    'count': count,
                    'date': index_date
                }
            )
            return Response(date_list)


class GoodsDayView(APIView):
    """
    日分类商品访问量
    """
    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取当天访问的商品分类数量信息
        data = GoodsVisitCount.objects.filter(date=now_date)
        # 序列化器返回分类数量
        ser = my_serializers.GoodsSerializer(data, many=True)
        return Response(ser.data)



